package com.gss.plugin.oauth2.config.token;

import com.gss.plugin.oauth2.config.Oauth2Properties;
import com.gss.plugin.oauth2.handler.JwtTokenEnhancer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

/**
 * @author DYang
 * @date 2023/8/20
 */
@ConditionalOnProperty(name = "stars.oauth-token.store-mode",havingValue = "jwt")
@Configuration
public class JwtTokenConfig {

    @Autowired
    private Oauth2Properties oauth2Properties;
    @Autowired
    private ApplicationContext applicationContext;


    @Bean
    public TokenStore jwtTokenStore(){
        JwtAccessTokenConverter jwtAccessTokenConverter = (JwtAccessTokenConverter) applicationContext.getBean("jwtAccessTokenConverter");
        return new JwtTokenStore(jwtAccessTokenConverter);
    }

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter(){
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey(oauth2Properties.getSignKey());
        return converter;
    }

    @Bean
    public JwtTokenEnhancer jwtTokenEnhancer(){
        return new JwtTokenEnhancer();
    }
}
